package com.raymond.queue.codec.decoding;

import com.raymond.queue.common.codec.annotation.MsgCodec;
import com.raymond.queue.common.codec.decoding.BaseDecoder;
import com.raymond.queue.common.constance.GlobalConstance;
import com.raymond.queue.common.enums.CommandIdType;
import com.raymond.queue.common.msg.BaseAgreement;

import com.raymond.queue.common.msg.request.ProductionMsgRequest;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;

import java.util.List;

/**
 * 消费者请求解码器
 *
 * @author :  raymond
 * @version :  V1.0
 * @date :  2021-07-14 11:39
 */
@MsgCodec(commandId = CommandIdType.PRODUCTION_REQ)
public class ProductionRequestDecoder extends BaseDecoder {

    @Override
    public void decode(ChannelHandlerContext ctx, BaseAgreement msg, List<Object> out) {
        ByteBuf bodyBuffer = Unpooled.wrappedBuffer(msg.getBodyBuffer());
        int length = bodyBuffer.readInt();
        String topic = bodyBuffer.readCharSequence(length, GlobalConstance.DEFAULT_TRANSPORT_CHARSET).toString();
        byte[] data = new byte[msg.getBodyLength() - length - 4];
        bodyBuffer.readBytes(data);
        out.add(new ProductionMsgRequest(topic, data, msg.getSequenceId()));
    }
}
